package workforce.limousine.struts;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import limousine.vo.Users;
import limousine.vo.Systems;
import limousine.util.Service;
import limousine.util.Token;

public class SystemsManagerQuery extends Action {

    private String order = "";

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession();
        System.out.println("***SystemsManagerInit");

        Users user = (Users) session.getAttribute(Token.USER_SESSION);
        if (user == null) {
            System.out.println("--SESSION EXPIRE--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }
       /* if (!user.getRole_name().equals("Admin")) {
            System.out.println("--Not Admin Hack--");
            return mapping.findForward(Token.SESSION_EXPIRE);
        }*/

        session.removeAttribute(Token.SYSTEMS);

        //        DataSource ds = this.getDataSource(request, Token.DATASOURCE_NAME);
        Service service = new Service();
        service.setServletContext(this.getServlet().getServletContext());
        DataSource ds = service.getDataSource();
        service.removeMsgAttribute(request, session, "");

        DynaActionForm f = (DynaActionForm) form;
        f.set("name", "");
        f.set("detail", "");


        order = request.getParameter("order");
        order = "order by " + ((order == null) ? "name" : order);

        List<Systems> l = getSystems(ds);
        System.out.println("size " + l.size());
        if (l.size() > 0) {
            session.setAttribute(Token.SYSTEMS, l);
        }


        service.updateControlPanel(session, ds, user.getRoleId());//update control panel display


        return mapping.findForward(Token.SUCCESS);
    }

    public List<Systems> getSystems(DataSource ds) {
        StringBuffer sql = new StringBuffer();
        sql.append("Select * From systems order by name");
        List<Systems> l = new ArrayList<Systems>();
        try {
            Connection conn = ds.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql.toString());
            while (rs.next()) {
                Systems o = new Systems();
                o.setId(rs.getString(1));
                o.setName(rs.getString(2));
                o.setDetail(rs.getString(3));
                o.setEnable(rs.getString(4));
                l.add(o);
            }
            rs.close();
            stmt.close();
            conn.close();
            return l;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
